import { getEntrustList,getTodoList, addEntrust, getEntrustById, deleteEntrustById, deleteDetailData, updateEntrustStatusById, entrustTask, getListDataForEntrust} from '../server/EntrustApi'

const initData = {
  entrustListData: [],
  totalCount: 0,
  todoListData: [],
  todoListTotalCount: 0,
  entrustDetailData: {},
};

export default {
  namespace: "Entrust",
  state: {
    ...initData,
  },
  effects: {
    *getEntrustListData({ payload }, { call, put }) {
      const { datas = [], totalCount = 0 } = yield call(getEntrustList, payload);
      yield put({ type: "get/entrustList", datas: datas || [], totalCount });
    },
    *getTodoListData({ payload }, { call, put }) {
      const { datas = [], totalCount = 0 } = yield call(getTodoList, payload);
      yield put({ type: "get/todoList", payload: datas || [], totalCount });
    },
    *addEntrustData({ payload }, { call }) {
      const res = yield call(addEntrust, payload);
      // const { errCode, errMsg, errortext, success, datas } = res && res.data;
      return res;
    },
    *getEntrustData({ payload }, { call, put }) {
      const { datas = [], totalCount = 0 } = yield call(getEntrustById, payload);
      return yield put({ type: "get/entrust", payload: datas || [], totalCount });
    },
    *deleteEntrust({ payload }, { call }) {
      const res = yield call(deleteEntrustById, payload);
      // const { errCode, errMsg, errortext, success, datas } = res && res.data;
      return res;
    },
    *deleteDetailDataList({ payload }, { call }) {
      const res = yield call(deleteDetailData, payload);
      return res;
    },
    *updateEntrustStatusById({ payload }, { call }) {
      const res = yield call(updateEntrustStatusById, payload);
      // const { errCode, errMsg, errortext, success, datas } = res && res.data;
      return res;
    },
    *entrustTaskData({ payload }, { call }) {
      const res = yield call(entrustTask, payload);
      // const { errCode, errMsg, errortext, success, datas } = res && res.data;
      return res;
    },
    *getListDataForEntrustData({ payload }, { call }) {
      const res = yield call(getListDataForEntrust, payload);
      // const { errCode, errMsg, errortext, success, datas } = res && res.data;
      return res;
    },
  },
  reducers: {
    "get/entrustList": (state, { datas, totalCount }) => {
      // 由于父子数据存在重复id，此处人为添加$id
      datas.forEach(item => {
        item.$id = `parent${item.id}`;
        (item.flowEntrustDetailVoList ? item.flowEntrustDetailVoList : []).forEach((ite) => {
          ite.$id = `child${ite.id}`;
        })
      })
      return {
        ...state,
        entrustListData: datas,
        totalCount,
      }
    },
    "get/todoList": (state, { payload, todoListTotalCount }) => ({
      ...state,
      todoListData: payload,
      todoListTotalCount,
    }),
    "get/entrust": (state, { payload }) => ({
      ...state,
      entrustDetailData: payload[0],
    }),
    go: (state, { payload }) => {
      return {
        ...state,
        entrustListData: payload,
      }
    },
    clearData: () => ({ ...initData }),
  },
};
